package com.jinzhi.jzweb.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.CertificateProfileDO;
import com.jinzhi.jzweb.domain.KnowledgePointsDO;
import com.jinzhi.jzweb.service.CertificateProfileService;
import com.jinzhi.jzweb.service.KnowledgePointsService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * <pre>
 * 证书概况
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/certificateProfile")
public class CertificateProfileController extends BaseController {
	@Autowired
	private CertificateProfileService certificateProfileService;
	@Autowired
	private UserService userService;
	@Autowired
	private KnowledgePointsService knowledgePointsService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:certificateProfile:certificateProfile")
	String CertificateProfile(){
	    return "jzweb/certificateProfile/certificateProfile";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:certificateProfile:certificateProfile")
	public Result<Page<CertificateProfileDO>> list(CertificateProfileDO certificateProfileDTO){
		// 查询列表数据
		Page<CertificateProfileDO> page = certificateProfileService.selectPage(getPage(CertificateProfileDO.class),
				certificateProfileService.convertToEntityWrapper("name", certificateProfileDTO.getName(),"state",certificateProfileDTO.getState())
						.orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			CertificateProfileDO obj = (CertificateProfileDO) page.getRecords().get(i);
			if (obj.getCreaterId() != null){
				UserDO userDO = userService.selectById(obj.getCreaterId());
				if (userDO != null) {
					obj.setCreaterName(userDO.getUsername());
				}

			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:certificateProfile:add")
	String add(){
	    return "jzweb/certificateProfile/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:certificateProfile:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CertificateProfileDO certificateProfile = certificateProfileService.selectById(id);
		model.addAttribute("certificateProfile", certificateProfile);
	    return "jzweb/certificateProfile/edit";
	}
	
	@Log("添加证书概况")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:certificateProfile:add")
	public Result<String> save( CertificateProfileDO certificateProfile){
		certificateProfile.setCreaterId(this.getUserId());
		boolean bb = certificateProfileService.insert(certificateProfile);
		if(bb){
			KnowledgePointsDO knowledgePointsDO = new KnowledgePointsDO();
			knowledgePointsDO.setPid(0L);//父级id
			knowledgePointsDO.setName("综合知识点");
			knowledgePointsDO.setLevel("1");//层级
			knowledgePointsDO.setGrade("高级,中级,初级");//等级
			knowledgePointsDO.setProfileId(certificateProfile.getId());//证书概况id
			knowledgePointsDO.setState(0);//默认正常
			knowledgePointsDO.setCreaterId(this.getUserId());//创建人id
			knowledgePointsService.insert(knowledgePointsDO);
		}
        return Result.ok();
	}
	
	@Log("修改证书概况")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:certificateProfile:edit")
	public Result<String>  update( CertificateProfileDO certificateProfile){
		boolean update = certificateProfileService.updateById(certificateProfile);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除证书概况")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:certificateProfile:remove")
	public Result<String>  remove( Long id){
		KnowledgePointsDO knowledgePointsDO = knowledgePointsService.findOneByKv("pid",0,"profile_id",id,"name","综合知识点");
		if(knowledgePointsDO != null){
			knowledgePointsService.deleteById(knowledgePointsDO.getId());
		}
		certificateProfileService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除证书概况")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:certificateProfile:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		if (ids != null && ids.length>0){
			for(Long id :ids){
				KnowledgePointsDO knowledgePointsDO = knowledgePointsService.findOneByKv("pid",0,"profile_id",id,"name","综合知识点");
				if(knowledgePointsDO != null){
					knowledgePointsService.deleteById(knowledgePointsDO.getId());
				}
			}
		}
		certificateProfileService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
